package com.example.servlet;

import com.example.dao.transactionDao;
import com.example.dao.transactionDaoImpl;
import com.example.dao.transactionDaoImpl;
import com.example.entity.transaction;

import com.example.entity.transaction;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import com.google.gson.Gson;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/getTransactionData")
public class transactionServlet extends HttpServlet {

    private transactionDao transactionDao;

    @Override
    public void init() throws ServletException {
        try {
            transactionDao = new transactionDaoImpl();
        } catch (SQLException e) {
            throw new ServletException("DAO 初始化失败", e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        String timeRange = request.getParameter("timeRange");
        String priceRange = request.getParameter("priceRange");

        System.out.println("Received request - timeRange: " + timeRange + ", priceRange: " + priceRange);

        List<transaction> data = null;

        try {
            switch (timeRange) {
                case "today":
                    switch (priceRange) {
                        case "all": data = transactionDao.getTodayByAll(); break;
                        case "0-100": data = transactionDao.getTodayBy0To100(); break;
                        case "100-1000": data = transactionDao.getTodayBy100To1000(); break;
                        case "above1000": data = transactionDao.getTodayByAbove1000(); break;
                    }
                    break;
                case "yesterday":
                    switch (priceRange) {
                        case "all": data = transactionDao.getYesterdayByAll(); break;
                        case "0-100": data = transactionDao.getYesterdayBy0To100(); break;
                        case "100-1000": data = transactionDao.getYesterdayBy100To1000(); break;
                        case "above1000": data = transactionDao.getYesterdayByAbove1000(); break;
                    }
                    break;
                case "this_week":
                    switch (priceRange) {
                        case "all": data = transactionDao.getThisWeekByAll(); break;
                        case "0-100": data = transactionDao.getThisWeekBy0To100(); break;
                        case "100-1000": data = transactionDao.getThisWeekBy100To1000(); break;
                        case "above1000": data = transactionDao.getThisWeekByAbove1000(); break;
                    }
                    break;
                case "this_month":
                    switch (priceRange) {
                        case "all": data = transactionDao.getThisMonthByAll(); break;
                        case "0-100": data = transactionDao.getThisMonthBy0To100(); break;
                        case "100-1000": data = transactionDao.getThisMonthBy100To1000(); break;
                        case "above1000": data = transactionDao.getThisMonthByAbove1000(); break;
                    }
                    break;
                default:
                    data = transactionDao.getTodayByAll();
            }

            if (data == null || data.isEmpty()) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "No data found");
                return;
            }

            response.setContentType("application/json;charset=UTF-8");
            String json = new Gson().toJson(data);
            response.getWriter().write(json);

        } catch (SQLException e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "数据库错误: " + e.getMessage());
        }
    }
}
